package com.cindata

import grails.transaction.Transactional

@Transactional
class AccountFlowStageNotificationService
{
    def initFlowStageNotification(Account account, Workflow workflow)
    {
        def workflowStages = WorkflowStage.findAllByWorkflow(workflow)
        for (
            workflowStage in
                workflowStages)
        {
            workflowStage?.notifications?.each {
                def accountFlowStageNotification = new AccountFlowStageNotification()
                accountFlowStageNotification.role = it?.role
                accountFlowStageNotification.messageTemplate = it?.messageTemplate
                accountFlowStageNotification.toManager = it?.toManager
                accountFlowStageNotification.cellphone = it?.cellphone
                accountFlowStageNotification.flowStage = AccountFlowStage.findByAccountAndStage(account, workflowStage?.stage)
                if (accountFlowStageNotification.validate())
                {
                    accountFlowStageNotification.save flush: true
                }
                else
                {
                    println accountFlowStageNotification.errors
                }
            }
        }
    }
}